package cn.db101.view;

import cn.db101.dao.bookDao;
import cn.db101.dao.shelfDao;
import cn.db101.model.book;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

public class admin_searchFrame {
	JFrame jf = new JFrame();
	private JPanel contentPane;
	private JTable table;
	private JTextField comboxtextField;
	private JComboBox comboBox;

	/**
	 * Create the frame.
	 */
	public  void init() {
		jf.setTitle("管理员主界面");
		
		jf.setIconImage(Toolkit.getDefaultToolkit().getImage(admin_searchFrame.class.getResource("/icon/Libraries_64px_1176502_easyicon.net.png")));
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setBounds(100, 100, 1278, 805);
		
		JMenuBar menuBar = new JMenuBar();
		jf.setJMenuBar(menuBar);
		
		JMenu m1 = new JMenu("主界面");
		m1.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/线性图书.png")));
		menuBar.add(m1);
		
		JMenu m2 = new JMenu("图书管理规定");
		m2.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/线性老师.png")));
	
		
		menuBar.add(m2);
		
		JMenu m3 = new JMenu(" 图书查询");
		m3.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/线性本子vg.png")));
		menuBar.add(m3);
		
		JMenu m4 = new JMenu("图书借还");
		m4.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/线性铅笔.png")));
		menuBar.add(m4);
		
		JMenu m5 = new JMenu("账户管理");
		m5.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/线性双指点击.png")));
		menuBar.add(m5);
		
		JMenu m6 = new JMenu("图书管理");
		m6.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/线性文件管理员.png")));
		menuBar.add(m6);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		jf.setContentPane(contentPane);
		
		JLayeredPane layeredPane = new JLayeredPane();
		contentPane.add(layeredPane, BorderLayout.CENTER);
		
		JLayeredPane layeredPane_1 = new JLayeredPane();
		layeredPane_1.setForeground(Color.BLACK);
		layeredPane_1.setBackground(Color.WHITE);
		layeredPane_1.setBounds(365, 155, 574, 117);
		layeredPane.add(layeredPane_1);
		
		 comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u6309\u4E66\u53F7\u67E5\u627E", "\u6309\u4E66\u540D\u67E5\u627E", "\u6309\u4F5C\u8005\u67E5\u627E", "\u6309isbn\u53F7\u67E5\u627E", "\u6309\u51FA\u7248\u793E\u67E5\u627E"}));
		comboBox.setBounds(79, 47, 121, 24);
		layeredPane_1.add(comboBox);
		
		comboxtextField = new JTextField();
		comboxtextField.setBounds(239, 47, 109, 24);
		layeredPane_1.add(comboxtextField);
		comboxtextField.setColumns(10);
		
		JButton button = new JButton("\u67E5\u627E");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				secAction(e);
			}
		});
		button.setForeground(Color.BLACK);
		button.setBackground(Color.WHITE);
		button.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/icon/\u5E95\u680F-\u6570\u636E\u67E5\u8BE21.png")));
		button.setBounds(403, 46, 113, 27);
		layeredPane_1.add(button);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(288, 302, 788, 386);
		layeredPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"书号", "isbn号", "书名", "作者", "出版社", "馆藏", "状态"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				true, true, true, true, true, true, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(table);

		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(admin_searchFrame.class.getResource("/img/find.png")));
		label.setBounds(0, 0, 1250, 722);
		layeredPane.add(label);
	
		m1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				closeThis();
				new admin_mainFrame().init();
			}
		});	
		m2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				closeThis();
				new admin_regulationFrame().init();
			}
		});	
	
		m4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				closeThis();
				new admin_borrow_mainFrame().init();
			}
		});	
		m5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				closeThis();
				new admin_account_mainFrame().init();
			}
		});	
		m6.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				closeThis();
				new bookmanage_mainFrame().init();
			}
		});	
		jf.setLocationRelativeTo(null);
		jf.setVisible(true);
		
	}
	protected void secAction(ActionEvent e) {
		int index = comboBox.getSelectedIndex();
		String string = comboxtextField.getText().toString();
		
		switch(index) {
			case 0:{
				ArrayList<book> arrayList = new bookDao().findID(string);
				createTable(arrayList);
				break;
			}
			case 1:{
				ArrayList<book> arrayList = new bookDao().findName(string);
				createTable(arrayList);
				break;
			}
			case 2:{
				ArrayList<book> arrayList = new bookDao().findWriter(string);
				createTable(arrayList);
				break;
			}
			case 3:{
				ArrayList<book> arrayList = new bookDao().findISBN(string);
				createTable(arrayList);
				break;
			}
			case 4:{
				ArrayList<book> arrayList = new bookDao().findPublisher(string);
				createTable(arrayList);
				break;
			}
			default:{
				break;
			}
		}
	}
	public void createTable(ArrayList<book> a) {
		DefaultTableModel sTableModel = (DefaultTableModel) table.getModel();
		// 清空表格
		sTableModel.setRowCount(0);
		for(book b : a) {
			Vector temp = new Vector();
			temp.add(b.getBook_id());
			temp.add(b.getIsbn());
			temp.add(b.getBook_name());
			temp.add(b.getWriter());
			temp.add(b.getPublisher());
			temp.add(new shelfDao().findLocation(b.getBook_shelf_no()));
			if (b.getBook_status() == 1) {
				temp.add("已借出");
			} else {
				temp.add("未借出");
			}
			sTableModel.addRow(temp);
		}
	}
	public  void closeThis() {
		jf.dispose();
	}
	public static void main(String[] args) {
		new admin_searchFrame().init();	
	}
}
